#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPixmap>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QDateTime>
#include <QCryptographicHash>
#include <QUuid>
#include <QDir>
#include <QMediaCaptureSession>
#include <QCamera>
#include <QImageCapture>
#include <QVideoWidget>
#include <QMediaDevices>

#include <QTableWidget>
#include <QHeaderView>

#include <QFuture>
#include <QtConcurrent>
#include <QTimer>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void resizeEvent(QResizeEvent *event) override;

private slots:
    void on_Btn_Picture_clicked();
    void on_Btn_Car_Inf_clicked();
    void handleApiReply(QNetworkReply *reply);
    void on_Btn_Shooting_clicked();
    void imageCaptured(int id, const QImage &preview);



private:
    Ui::MainWindow *ui;
    QPixmap currentImage;
    QNetworkAccessManager *networkManager;

    // 车辆信息结构体
    struct CarRecord {
        QString plateNumber;
        QString brand;
        QString series;
        QString type;
        QString color;
        QString enterTime;
        QString leaveTime;
        bool isActive;
    };

    QList<CarRecord> carRecords;  // 存储车辆记录

    // 添加线程安全标志
    QAtomicInt capturing = 0;
    QTimer *cameraTimer;
    QFutureWatcher<QPixmap> *imageScalingWatcher;

    // 添加函数
    void updateTable();
    void saveCarInfo(const QString& plateNumber, const QString& brand,
                     const QString& series, const QString& type,
                     const QString& color, bool isEntering);




    QCamera *camera = nullptr;
    QMediaCaptureSession captureSession;
    QImageCapture *imageCapture = nullptr;
    QVideoWidget *videoWidget = nullptr;

    const QString secretId = "AKIDCHsR5qT3c9d1FG5lIk2CAwUu9BzeATZp";
    const QString secretKey = "MXVHuLdlkZBamodFdS8ex41lKjy4ziEZ";
    const QString endpoint = "tiia.tencentcloudapi.com";
    const QString service = "tiia";
    const QString region = "ap-guangzhou";
    const QString version = "2019-05-29";
    const QString action = "RecognizeCarPro";

    QString generateTc3Signature(const QByteArray &payload,qint64 timestamp);


    QTcpSocket *tcpSocket;
    const QString serverIP = "192.168.3.4"; // 服务器IP
    const int serverPort = 8888;          // 服务器端口

};

#endif // MAINWINDOW_H
